import 'package:daoyi_ui_admin_getx/app/common/utils/common_picker_util.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class CommonFormItemWidget extends StatelessWidget {
  final String label;
  final Widget Function(BuildContext? context)? contentBuilder;
  final Widget? suffix;
  final String? suffixText;
  final String? hintText;
  final ValueChanged? onChanged;
  final TextEditingController? controller;

  const CommonFormItemWidget({
    super.key,
    this.label = '',
    this.contentBuilder,
    this.suffix,
    this.suffixText,
    this.hintText,
    this.onChanged,
    this.controller,
  });

  factory CommonFormItemWidget.selectPage({
    final String label = '',
    final String hintText = '',
    final String page = '',
  }) =>
      CommonFormItemWidget(
        label: label,
        contentBuilder: (context) {
          return GestureDetector(
            onTap: () {
              Get.toNamed(page);
            },
            behavior: HitTestBehavior.translucent,
            child: SizedBox(
              height: 40,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    hintText,
                    style: TextStyle(fontSize: 16, color: Get.theme.hintColor),
                  ),
                  const Icon(Icons.chevron_right),
                ],
              ),
            ),
          );
        },
      );

  factory CommonFormItemWidget.text({
    final String label = '',
    final String hintText = '',
    final TextEditingController? controller,
  }) =>
      CommonFormItemWidget(
        label: label,
        hintText: hintText,
        controller: controller,
      );

  factory CommonFormItemWidget.radio({
    final String label = '',
    final List<String> options = const [],
    final int value = 0,
    final ValueChanged? onChanged,
  }) =>
      CommonFormItemWidget(
        label: label,
        contentBuilder: (context) {
          return SizedBox(
            height: 40,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: List.generate(
                options.length,
                (index) => Row(
                  children: [
                    Radio(
                      value: index,
                      groupValue: value,
                      onChanged: onChanged,
                    ),
                    Text(
                      options[index],
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      );

  factory CommonFormItemWidget.select({
    final String label = '',
    final String hintText = '',
    final List<String> options = const [],
    final int value = 0,
    final ValueChanged? onChanged,
  }) =>
      CommonFormItemWidget(
        label: label,
        contentBuilder: (context) {
          return GestureDetector(
            onTap: () {
              CommonPickerUtil.showPicker(
                context: context!,
                options: options,
                value: value,
                height: Get.height / 2,
              ).then(
                (selectedVal) {
                  if (selectedVal != null) {
                    onChanged!(selectedVal);
                  }
                },
              );
            },
            behavior: HitTestBehavior.translucent,
            child: SizedBox(
              height: 40,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    options[value],
                  ),
                  const Icon(Icons.chevron_right),
                ],
              ),
            ),
          );
        },
      );

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(left: 14, right: 14),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 1,
            color: Get.theme.dividerColor,
          ),
        ),
      ),
      child: Row(
        children: [
          SizedBox(
            width: 120,
            child: Text(
              label != '' ? '$label：' : '',
              style: const TextStyle(
                fontSize: 16,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          Expanded(
            child: contentBuilder != null
                ? contentBuilder!(context)
                : TextField(
                    controller: controller,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: hintText,
                    ),
                  ),
          ),
          if (suffix != null) suffix!,
          if (suffix == null && suffixText != null) Text(suffixText!),
        ],
      ),
    );
  }
}
